package com.xz.xzutils.utils;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderEnumerator;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025-02-12 16:52
 */
public class MyBatisDetector {
    private MyBatisDetector() {
    }

    /**
     * 检查是否使用了 mybatis
     * @param project 当前项目
     * @return 是否使用了 mybatis
     */
    public static boolean isMyBatisUsed(Project project) {
        return hasMybatisDependency(project);
    }

    // 检查依赖配置项
    public static boolean hasMybatisDependency(Project project) {
        AtomicBoolean flag = new AtomicBoolean(false);
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            OrderEnumerator.orderEntries(module).forEachLibrary(library -> {
                if (library == null || library.getName() == null) {
                    return true;
                }
                boolean b = library.getName().toLowerCase().contains("mybatis-spring-boot-starter")
                        || library.getName().toLowerCase().contains("mybatis-spring-boot-autoconfigure")
                        || library.getName().toLowerCase().contains("mybatis-spring")
                        || library.getName().toLowerCase().contains("mybatis");
                flag.set(flag.get() || b);
                return true;
            });
        }
        return flag.get();
    }
}
